﻿using System.Windows.Controls;
using System.Windows.Input;
using dIRca.WP7.Extensions;
using dIRca.WP7.ViewModels;
using Microsoft.Phone.Controls;

namespace dIRca
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void textBoxSendMessage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // because binding does not occure untile you leave the box we want to force it here
                ((TextBox)sender).ForceBinding();
                // swap focus back to the screen
                //Focus();
                var viewModel = DataContext as MainViewModel;
                viewModel.SendCommand.Execute(null);
            }
        }

        private void MessagesSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((sender != null) && (e.AddedItems.Count != 0))
            {
                var control = ((ListBox)sender);
                UpdateLayout();
                control.ScrollIntoView(control.SelectedItem);
            }
        }
    }
}